package cn.community.care.module.system.controller.admin.illnesshistory;

import cn.community.care.module.system.controller.admin.illnesshistory.vo.IllnessHistoryPageReqVO;
import cn.community.care.module.system.controller.admin.illnesshistory.vo.IllnessHistoryRespVO;
import cn.community.care.module.system.controller.admin.illnesshistory.vo.IllnessHistorySaveReqVO;
import cn.community.care.module.system.dal.dataobject.illnesshistory.IllnessHistoryDO;
import cn.community.care.module.system.service.illnesshistory.IllnessHistoryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;
import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 用户生病历史")
@RestController
@RequestMapping("/user/illness-history")
@Validated
public class IllnessHistoryController {

    @Resource
    private IllnessHistoryService illnessHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建用户生病历史")
    public CommonResult<Long> createIllnessHistory(@Valid @RequestBody IllnessHistorySaveReqVO createReqVO) {
        return success(illnessHistoryService.createIllnessHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户生病历史")
    public CommonResult<Boolean> updateIllnessHistory(@Valid @RequestBody IllnessHistorySaveReqVO updateReqVO) {
        illnessHistoryService.updateIllnessHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户生病历史")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteIllnessHistory(@RequestParam("id") Long id) {
        illnessHistoryService.deleteIllnessHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户生病历史")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<IllnessHistoryRespVO> getIllnessHistory(@RequestParam("id") Long id) {
        IllnessHistoryDO illnessHistory = illnessHistoryService.getIllnessHistory(id);
        return success(BeanUtils.toBean(illnessHistory, IllnessHistoryRespVO.class));
    }

    @GetMapping("/get/by/userID")
    @Operation(summary = "获得用户生病历史")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<IllnessHistoryRespVO> getIllnessHistoryByUserId(@RequestParam("id") Long id) {
        IllnessHistoryDO illnessHistory = illnessHistoryService.getIllnessHistoryByUserId(id);
        return success(BeanUtils.toBean(illnessHistory, IllnessHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户生病历史分页")
    public CommonResult<PageResult<IllnessHistoryRespVO>> getIllnessHistoryPage(@Valid IllnessHistoryPageReqVO pageReqVO) {
        PageResult<IllnessHistoryDO> pageResult = illnessHistoryService.getIllnessHistoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IllnessHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户生病历史 Excel")
    @OperateLog(type = EXPORT)
    public void exportIllnessHistoryExcel(@Valid IllnessHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IllnessHistoryDO> list = illnessHistoryService.getIllnessHistoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户生病历史.xls", "数据", IllnessHistoryRespVO.class,
                        BeanUtils.toBean(list, IllnessHistoryRespVO.class));
    }

}